package com.dragons.dragonsvf.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dragons.dragonsvf.dao.UserInfoMapper;
import com.dragons.dragonsvf.entities.ResponseDTO;
import com.dragons.dragonsvf.entities.UserDTO;
import com.dragons.dragonsvf.entities.UserInfo;
import com.dragons.dragonsvf.utils.idbuilder.IdUtil;
import com.dragons.dragonsvf.utils.secret.AesEncryptUtil;
import com.dragons.dragonsvf.utils.secret.RSAEncryption;
import com.google.errorprone.annotations.Var;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.KeyPair;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author dragons
 * @version 1.0.0
 * @description: 封装注册请求
 * @date 2025/3/13 16:24
 */
@RestController
public class RegisterCtrl {

    private static final Logger logger = LoggerFactory.getLogger(RegisterCtrl.class);

    @Autowired
    private UserInfoMapper userInfoMapper;

    @RequestMapping("/checkUser")
    public ResponseDTO checkUser(@RequestBody UserDTO user) {
        Map<String,Object> usermap = new HashMap<>();
        usermap.put("username",user.getuName());
        List<UserInfo> userInfos = userInfoMapper.selectByMap(usermap);
        ResponseDTO responseDTO = new ResponseDTO();
        if (userInfos.size() > 0) {
            responseDTO.setCode(200);
            responseDTO.setMsg("用户名已存在!");
        } else {
            responseDTO.setCode(200);
            responseDTO.setMsg("用户名可用!");
            responseDTO.setData(0);
        }
        return responseDTO;
    }

    /** 用户注册 */
    @RequestMapping("/register")
    public ResponseDTO login(@RequestBody UserDTO user) {

        // 将注册信息插入到用户表中 加密密码
        UserInfo userInfo = new UserInfo();
        userInfo.setId(IdUtil.snowflakeId(32)); // 唯一ID
        userInfo.setUsername(user.getuName()); // 用户名
        // 密码AES加密
        String aesEn = "";
        try {
            aesEn = AesEncryptUtil.encrypt(user.getuPwdword());
        } catch (Exception e) {
            e.printStackTrace();
        }
        userInfo.setAcross_num(user.getuPwdword());// 原始密码
        userInfo.setEncrypt_pwd(aesEn);// AES 加密后密码
        userInfo.setEncryption_algorithm("AES"); // 加密算法
        userInfo.setEmail(user.getuEmail()); // 电子邮箱
        userInfo.setTel_num(user.getuCellphone()); // 电话
        userInfo.setWechat_abt(user.getuWechat()); // 微信
        // 头像 地址
        userInfo.setAvatar_url(user.getuNetavator_url());
        userInfo.setAvatar_url(user.getuUploadavator_url());
//        userInfo.setAvatar_url("".equals(user.getuNetavator_url()) ? user.getuNetavator_url() :
//                "".equals(user.getuUploadavator_url()) ? user.getuUploadavator_url() : "");
        userInfo.setRole_num("1"); // 角色编号
        userInfo.setFreeze_ornnt("0"); // 是否冻结
        userInfo.setCreate_tm(new Timestamp(System.currentTimeMillis())); // 创建时间
        userInfo.setUpdate_tm(new Timestamp(System.currentTimeMillis())); // 最新修改时间
        int result = userInfoMapper.insert(userInfo);

        ResponseDTO responseDTO = new ResponseDTO();
        if (result > 0) {
            responseDTO.setCode(200);
            responseDTO.setMsg("注册成功");
        } else {
            responseDTO.setCode(500);
            responseDTO.setMsg("注册失败");
        }
        return responseDTO;
    }




}
